package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.after.sale.api.feign.ReplaceGoodsControllerFeign;
import com.ssy.lingxi.after.sale.api.feign.ReturnGoodsControllerFeign;
import com.ssy.lingxi.after.sale.api.model.vo.request.AddLogisticsVO;
import com.ssy.lingxi.after.sale.api.model.vo.request.VerifyLogisticsVO;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.enhance.api.feign.ProduceNoticeOrderControllerFeign;
import com.ssy.lingxi.enhance.api.model.dto.AddLogisticsDTO;
import com.ssy.lingxi.enhance.api.model.dto.ConfirmLogisticsDTO;
import com.ssy.lingxi.logistics.entity.LogisticsOrder;
import com.ssy.lingxi.logistics.service.ILogisticsOrderSyncDataService;
import com.ssy.lingxi.order.api.feign.OrderFeignService;
import com.ssy.lingxi.order.api.model.vo.request.OrderUpdateLogisticsNoFeignVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 物流单同步数据服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/1/26
 */
@Slf4j
@Service
public class LogisticsOrderSyncDataServiceImpl implements ILogisticsOrderSyncDataService {

    @Resource
    private OrderFeignService orderFeignService;
    @Resource
    private ProduceNoticeOrderControllerFeign produceNoticeOrderControllerFeign;
    @Resource
    private ReplaceGoodsControllerFeign replaceGoodsControllerFeign;
    @Resource
    private ReturnGoodsControllerFeign returnGoodsControllerFeign;

    @Override
    public Wrapper<?> addLogisticsSyncDataSales(LogisticsOrder logisticsOrder) {
        if(logisticsOrder.getRelevanceOrderId() == null || logisticsOrder.getRelevanceOrderId().compareTo(0L) <= 0) {
            return Wrapper.success();
        }

        OrderUpdateLogisticsNoFeignVO feignVO = new OrderUpdateLogisticsNoFeignVO();
        feignVO.setOrderId(logisticsOrder.getRelevanceOrderId());
        feignVO.setLogisticsNo(logisticsOrder.getLogisticsOrderNo());
        return orderFeignService.updateOrderLogisticsNo(feignVO);
    }

    @Override
    public Wrapper<?> confirmLogisticsSyncDataSales(UserLoginCacheDTO loginUser, LogisticsOrder logisticsOrder, Integer status) {
        if(logisticsOrder.getRelevanceOrderId() == null || logisticsOrder.getRelevanceOrderId().compareTo(0L) <= 0) {
            return Wrapper.success();
        }

        OrderUpdateLogisticsNoFeignVO feignVO = new OrderUpdateLogisticsNoFeignVO();
        feignVO.setOrderId(logisticsOrder.getRelevanceOrderId());
        feignVO.setLogisticsNo(logisticsOrder.getLogisticsOrderNo());
        return orderFeignService.updateOrderLogisticsNo(feignVO);
    }

    @Override
    public Wrapper<?> addLogisticsSyncDataProduction(LogisticsOrder logisticsOrder) {
        AddLogisticsDTO addLogisticsDTO = new AddLogisticsDTO();
        // 生产通知单id
        addLogisticsDTO.setProduceNoticeOrderId(logisticsOrder.getRelevanceOrderId());
        addLogisticsDTO.setLogisticsOrderId(logisticsOrder.getId());
        addLogisticsDTO.setLogisticsOrderNo(logisticsOrder.getLogisticsOrderNo());
        addLogisticsDTO.setCompanyName(logisticsOrder.getCompanyName());

        return produceNoticeOrderControllerFeign.submit(addLogisticsDTO);
    }

    @Override
    public Wrapper<?> confirmLogisticsSyncDataProduction(UserLoginCacheDTO loginUser, LogisticsOrder logisticsOrder, Integer status) {
        ConfirmLogisticsDTO confirmLogisticsDTO = new ConfirmLogisticsDTO();
        confirmLogisticsDTO.setStatus(status);
        confirmLogisticsDTO.setProduceNoticeOrderId(logisticsOrder.getRelevanceOrderId());
        confirmLogisticsDTO.setLogisticsOrderNo(logisticsOrder.getLogisticsOrderNo());
        confirmLogisticsDTO.setUser(loginUser);

        return produceNoticeOrderControllerFeign.confirmLogistics(confirmLogisticsDTO);
    }

    @Override
    public Wrapper<?> addLogisticsSyncDataReplaceReturn(UserLoginCacheDTO loginUser, LogisticsOrder logisticsOrder) {
        AddLogisticsVO addLogisticsVO = new AddLogisticsVO();
        // 换货发货单id
        addLogisticsVO.setDataId(logisticsOrder.getRelevanceOrderId());
        addLogisticsVO.setLogisticsId(logisticsOrder.getId());
        addLogisticsVO.setLogisticsOrderNo(logisticsOrder.getLogisticsOrderNo());
        addLogisticsVO.setLogisticsName(logisticsOrder.getCompanyName());
        addLogisticsVO.setMember(loginUser);
        return replaceGoodsControllerFeign.addReturnLogistics(addLogisticsVO);
    }

    @Override
    public Wrapper<?> confirmLogisticsSyncDataReplaceReturn(UserLoginCacheDTO loginUser, LogisticsOrder logisticsOrder, Integer status) {
        VerifyLogisticsVO verifyVO = new VerifyLogisticsVO();
        verifyVO.setIsAccept(status);
        verifyVO.setDataId(logisticsOrder.getRelevanceOrderId());
        verifyVO.setMember(loginUser);

        return replaceGoodsControllerFeign.verifyReturnLogistics(verifyVO);
    }

    @Override
    public Wrapper<?> addLogisticsSyncDataReplaceReplace(UserLoginCacheDTO loginUser, LogisticsOrder logisticsOrder) {
        AddLogisticsVO addLogisticsVO = new AddLogisticsVO();
        // 换货发货单id
        addLogisticsVO.setDataId(logisticsOrder.getRelevanceOrderId());
        addLogisticsVO.setLogisticsId(logisticsOrder.getId());
        addLogisticsVO.setLogisticsOrderNo(logisticsOrder.getLogisticsOrderNo());
        addLogisticsVO.setLogisticsName(logisticsOrder.getCompanyName());
        addLogisticsVO.setMember(loginUser);
        return replaceGoodsControllerFeign.addReplaceLogistics(addLogisticsVO);
    }

    @Override
    public Wrapper<?> confirmLogisticsSyncDataReplaceReplace(UserLoginCacheDTO loginUser, LogisticsOrder logisticsOrder, Integer status) {
        VerifyLogisticsVO verifyVO = new VerifyLogisticsVO();
        verifyVO.setIsAccept(status);
        verifyVO.setDataId(logisticsOrder.getRelevanceOrderId());
        verifyVO.setMember(loginUser);

        return replaceGoodsControllerFeign.verifyReplaceLogistics(verifyVO);
    }

    @Override
    public Wrapper<?> addLogisticsSyncDataReturnReturn(UserLoginCacheDTO loginUser, LogisticsOrder logisticsOrder) {
        AddLogisticsVO addLogisticsVO = new AddLogisticsVO();
        // 退货发货单id
        addLogisticsVO.setDataId(logisticsOrder.getRelevanceOrderId());
        addLogisticsVO.setLogisticsId(logisticsOrder.getId());
        addLogisticsVO.setLogisticsOrderNo(logisticsOrder.getLogisticsOrderNo());
        addLogisticsVO.setLogisticsName(logisticsOrder.getCompanyName());
        addLogisticsVO.setMember(loginUser);
        return returnGoodsControllerFeign.addReturnLogistics(addLogisticsVO);
    }

    @Override
    public Wrapper<?> confirmLogisticsSyncDataReturnReturn(UserLoginCacheDTO loginUser, LogisticsOrder logisticsOrder, Integer status) {
        VerifyLogisticsVO verifyVO = new VerifyLogisticsVO();
        verifyVO.setIsAccept(status);
        verifyVO.setDataId(logisticsOrder.getRelevanceOrderId());
        verifyVO.setMember(loginUser);

        return returnGoodsControllerFeign.verifyReturnLogistics(verifyVO);
    }
}